package model.impl;

import java.util.Date;
import java.util.List;

import model.bo.Article;
import model.dao.IArticleDAO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Data access object (DAO) for domain model class Article.
 * 
 * @see model.bo.Article
 * @author MyEclipse Persistence Tools
 */

public class ArticleDAO extends HibernateDaoSupport implements IArticleDAO {
	private static final Log log = LogFactory.getLog(ArticleDAO.class);

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see model.bo.IArticleDAO#save(model.bo.Article)
	 */
	public void save(Article transientInstance) {
		log.debug("saving Article instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.bo.IArticleDAO#delete(model.bo.Article)
	 */
	public void delete(Article persistentInstance) {
		log.debug("deleting Article instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.bo.IArticleDAO#findById(java.lang.Integer)
	 */
	public Article findById(java.lang.Integer id) {
		log.debug("getting Article instance with id: " + id);
		try {
			Article instance = (Article) getHibernateTemplate().get(
					"model.bo.Article", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.bo.IArticleDAO#findByExample(model.bo.Article)
	 */
	public List findByExample(Article instance) {
		log.debug("finding Article instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.bo.IArticleDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Article instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Article as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.bo.IArticleDAO#findByTitle(java.lang.Object)
	 */
	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	/* (non-Javadoc)
	 * @see model.bo.IArticleDAO#findByContent(java.lang.Object)
	 */
	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	/* (non-Javadoc)
	 * @see model.bo.IArticleDAO#findByAuthor(java.lang.Object)
	 */
	public List findByAuthor(Object author) {
		return findByProperty(AUTHOR, author);
	}

	/* (non-Javadoc)
	 * @see model.bo.IArticleDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all Article instances");
		try {
			String queryString = "from Article";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.bo.IArticleDAO#merge(model.bo.Article)
	 */
	public Article merge(Article detachedInstance) {
		log.debug("merging Article instance");
		try {
			Article result = (Article) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.bo.IArticleDAO#attachDirty(model.bo.Article)
	 */
	public void attachDirty(Article instance) {
		log.debug("attaching dirty Article instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.bo.IArticleDAO#attachClean(model.bo.Article)
	 */
	public void attachClean(Article instance) {
		log.debug("attaching clean Article instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IArticleDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IArticleDAO) ctx.getBean("ArticleDAO");
	}
}